from .sers import *
from rest_framework.views import APIView
from rest_framework.response import Response


class IndexView(APIView):
    def get(self, request):
        # 查询出所有的分类
        queryset = Category.objects.all()
        ser = CategorySer(queryset, many=True)
        data_list = []
        for item in queryset:
            # 创建一个临时字典，存放一个分类信息
            temp = {}
            # 查询分类下的所有商品
            goods_queryset = Goods.objects.filter(cate_id=item.id)
            temp["cate"] = CategorySer(item).data
            temp["goods_list"] = GoodsListSer(goods_queryset, many=True).data
            # 把一个分类下的所有信息存入到列表
            data_list.append(temp)
        print(data_list)
        return Response(data_list)


class GoodsDetailView(APIView):
    def get(self, request):
        id = request.GET.get("id")
        goods = Goods.objects.filter(id=id).first()
        ser = GoodsSer(goods)
        return Response(ser.data)

